/***
    Amrita ITEWS - Copyright (C) 2009 Amrita Vishwa Vidyapeetham, Amritapuri.
                                       (http://www.amrita.edu)
    ***************************************************************************
    This file is part of the Amrita ITEWS distribution.
    Amrita ITEWS is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License (version 2) as published
    by the Free Software Foundation AND MODIFIED BY the Amrita ITEWS exception.
    ***NOTE*** The exception to the GPL is included to allow you to distribute
    a combined work that includes Amrita ITEWS without being obliged to provide
    the source code for proprietary components outside of the Amrita ITEWS
    software. Amrita ITEWS is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    Public License for more details. You should have received a copy of the GNU
    General Public License and the Amrita ITEWS license exception along with
    Amrita ITEWS if not then it can be viewed here:
    http://itews.amrita.ac.in/license.html.


    Documentation, latest information, license and contact details are at:
    http://itews.amrita.ac.in/


    Amrita ITEWS source code can be found at:
    http://code.google.com/p/itews


    The initial code-base of Amrita ITEWS was developed by Amrita Vishwa
    Vidyapeetham as part of the project titled,"Intelligent & Interactive
    Telematics using Emerging Wireless Technologies for Transport Systems
    (ITEWS)" sponsored by Technology Information Forecasting and Assessment
    Council (TIFAC), India.
***/

/*! \file serial_eventq.c
    \brief Serial Driver (LPC) Event Queue Service Provider Interface Implementation.
*/
#include <typedefs.h>
#include <serial_eventq.h>
#include <serial.h>

/*Serial EventQ Operations Structure Variable Initialization.*/
eventq_ops_t serial_eventq_ops = {
                                serial_eventq_open,
                                serial_eventq_close,
                                serial_eventq_sync_get,
                                serial_eventq_sync_put,
                                0,
                                0
                            };

/*! \brief Eventq Open Operation.
    \param e_ctx Event Queue Context Structure Pointer.
    \param eventq_name Name of Event Queue to be Opened.
    \return 0 if success else error codes.
    */
STATUS serial_eventq_open( eventq_ctx_t *e_ctx, const char *eventq_name )
{
    return 0;
}

/*! \brief Eventq Close Operation.
    \param e_ctx Event Queue Context Structure Pointer.
    \return 0 if success else error codes.
    */
STATUS serial_eventq_close( eventq_ctx_t *e_ctx )
{
    STATUS status;

    status = serial_close(e_ctx->ctx_data);
    return status;
}

/*! \brief Eventq Synchronous Put Operation.
    \param e_ctx Event Queue Context Structure Pointer.
    \param e Event to put in Event Queue.
    \param res Result of Put Operation of the event e.
    \return 0 if success else error codes.
    */
STATUS serial_eventq_sync_put( eventq_ctx_t *e_ctx, event_t *e, HRESULT *res )
{
    int32               status;
    serial_param_t      *p;

    switch( e->type )
    {
        case EVT_SERIAL_OPEN:
            p = (serial_param_t *)e->param;
            e_ctx->ctx_data = serial_open(  p->port, p->mode );
            status = e_ctx->ctx_data;
            break;
        case EVT_SERIAL_PUT_DATA:
            status = serial_write(e_ctx->ctx_data, (int8 *)e->buf, e->ndata );
            break;
        default:
            return -1;
    }
    return status;
}

/*! \brief Eventq Synchronous Get Operation.
    \param e_ctx Event Queue Context Structure Pointer.
    \param e Event to get from Event Queue.
    \return event status if success else error codes.
    */
STATUS serial_eventq_sync_get( eventq_ctx_t *e_ctx, event_t *e )
{
    int32   status;

    switch( e->type )
    {
        case EVT_SERIAL_GET_DATA:
            e->ndata = serial_read(e_ctx->ctx_data, (int8 *)e->buf, e->ndata );//jayaraj - changed nbuf to ndata
            status = e->ndata;
            if(e->ndata == 0)
            {
                e->param = SERIAL_READ_TIMEOUT;
            }
            break;
        case EVT_SERIAL_TIMED_GET_DATA:
            e->ndata = serial_timed_read(e_ctx->ctx_data, (int8 *)e->buf, e->ndata, (uint32)e->param );//jayaraj - changed nbuf to ndata
            status = e->ndata;
            if(e->ndata == 0)
            {
                e->param = SERIAL_READ_TIMEOUT;
            }
            break;
        default:
            return -1;
    }
    return status;
}

